{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding:utf-8\n",
    "#\n",
    "# The MIT License (MIT)\n",
    "#\n",
    "# Copyright (c) 2018-2020 azai/Rgveda/GolemQuant\n",
    "#\n",
    "# Permission is hereby granted, free of charge, to any person obtaining a copy\n",
    "# of this software and associated documentation files (the \"Software\"), to deal\n",
    "# in the Software without restriction, including without limitation the rights\n",
    "# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n",
    "# copies of the Software, and to permit persons to whom the Software is\n",
    "# furnished to do so, subject to the following conditions:\n",
    "#\n",
    "# The above copyright notice and this permission notice shall be included in\n",
    "# all\n",
    "# copies or substantial portions of the Software.\n",
    "#\n",
    "# THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n",
    "# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n",
    "# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE\n",
    "# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n",
    "# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n",
    "# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n",
    "# SOFTWARE.\n",
    "\"\"\"\n",
    "基于 QUANTAXIS 的 DataStruct.add_func 使用，也可以单独使用处理 Kline数据，基于RSRS择时，\n",
    "RSRS(阻力支撑相对强度)择时策略研究，\n",
    "聚宽： https://www.joinquant.com/view/community/detail/df07581179e8a3c73e46978cb52d7448?type=best\n",
    "\"\"\"\n",
    "\n",
    "# 阻力支撑相对强度RSRS是一个强趋势策略，最适合单边牛市，同时具备逃顶能力，能抵抗类似类似15 16年熔断的股灾。\n",
    "\n",
    "# 详细了解：https://zhuanlan.zhihu.com/p/33501881\n",
    "\n",
    "# 通过和网格策略的有机结合还可以适应震荡市行情。\n",
    "\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "import os\n",
    "import datetime\n",
    "import numpy as np\n",
    "import statsmodels.formula.api as sml\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.stats as scs\n",
    "import matplotlib.mlab as mlab\n",
    "\n",
    "# import statsmodels.api as sm\n",
    "# from pandas.stats.api import ols\n",
    "try:\n",
    "    import QUANTAXIS as QA\n",
    "    from QUANTAXIS.QAIndicator.talib_numpy import *\n",
    "except:\n",
    "    print('PLEASE run \"pip install QUANTAXIS\" to call these modules')\n",
    "    pass\n",
    "\n",
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def pre_rsrs_data_func(data, N=18, M=252):\n",
    "    \"\"\"\n",
    "    准备数据\n",
    "    \"\"\"\n",
    "    highs = data.high.values\n",
    "    lows = data.low.values\n",
    "    start_t = datetime.datetime.now()\n",
    "    print(start_t)\n",
    "\n",
    "    # 斜率\n",
    "    data['beta'] = 0\n",
    "    data['R2'] = 0\n",
    "    beta_rsquared = np.zeros((len(data), 2), )\n",
    "\n",
    "    for i in range(len(highs))[N:]:\n",
    "        data_high = highs[i - N:i]\n",
    "        data_low = lows[i - N:i]\n",
    "        X = sm.add_constant(data_low)\n",
    "        model = sm.OLS(data_high, X)\n",
    "        results = model.fit()\n",
    "\n",
    "        # beta = low\n",
    "        if (len(results.params) > 1):\n",
    "            beta_rsquared[i, 0] = results.params[1]\n",
    "        else:\n",
    "            beta_rsquared[i, 0] = results.params[0]\n",
    "        beta_rsquared[i, 1] = results.rsquared\n",
    "\n",
    "    data[['beta', 'R2']] = beta_rsquared\n",
    "\n",
    "    # 日收益率\n",
    "    data['ret'] = data.close.pct_change(1)\n",
    "\n",
    "    # 标准分\n",
    "    data['beta_norm'] = data['beta'].rolling(M).apply(lambda x: scs.zscore(x.values)[-1])\n",
    "\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    data.iloc[:min(M, len(highs)), beta_norm] = scs.zscore(data.iloc[:min(M, len(highs)), beta].values)\n",
    "    data['RSRS_R2'] = data.beta_norm * data.R2\n",
    "    data = data.fillna(0)\n",
    "\n",
    "    # 右偏标准分\n",
    "    data['beta_right'] = data.RSRS_R2 * data.beta\n",
    "\n",
    "    end_t = datetime.datetime.now()\n",
    "    print(end_t, 'spent:{}'.format((end_t - start_t)))\n",
    "    return data\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def pre_rsrs_data_v1_func(data, N=18, M=252):\n",
    "    \"\"\"\n",
    "    准备数据\n",
    "    \"\"\"\n",
    "    highs = data.high\n",
    "    start_t = datetime.datetime.now()\n",
    "    print(start_t)\n",
    "\n",
    "    # 斜率\n",
    "    data['beta'] = 0\n",
    "    data['R2'] = 0\n",
    "    beta_rsquared = np.zeros((len(data), 2), )\n",
    "\n",
    "    for i in range(N - 1, len(highs) - 1):\n",
    "        # for i in range(len(highs))[N:]:\n",
    "        df_ne = data.iloc[i - N + 1:i + 1, :]\n",
    "        model = sml.ols(formula='high~low', data=df_ne)\n",
    "        result = model.fit()\n",
    "\n",
    "        # beta = low\n",
    "        beta_rsquared[i + 1, 0] = result.params[1]\n",
    "        beta_rsquared[i + 1, 1] = result.rsquared\n",
    "\n",
    "    data[['beta', 'R2']] = beta_rsquared\n",
    "\n",
    "    # 日收益率\n",
    "    data['ret'] = data.close.pct_change(1)\n",
    "\n",
    "    # 标准分\n",
    "    data['beta_norm'] = (data['beta'] - data.beta.rolling(M).mean().shift(1)) / data.beta.rolling(M).std().shift(1)\n",
    "\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    for i in range(min(M, len(highs))):\n",
    "        data.iat[i, beta_norm] = (data.iat[i, beta] - data.iloc[:i - 1, beta].mean()) / data.iloc[:i - 1,\n",
    "                                                                                        beta].std() if (\n",
    "                    data.iloc[:i - 1, beta].std() != 0) else np.nan\n",
    "\n",
    "    data.iat[2, beta_norm] = 0\n",
    "    data['RSRS_R2'] = data.beta_norm * data.R2\n",
    "    data = data.fillna(0)\n",
    "\n",
    "    # 右偏标准分\n",
    "    data['beta_right'] = data.RSRS_R2 * data.beta\n",
    "    end_t = datetime.datetime.now()\n",
    "    print(end_t, 'spent:{}'.format((end_t - start_t)))\n",
    "    return data\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def pre_rsrs_data_v2_func(data, N=18, M=252):\n",
    "    \"\"\"\n",
    "    准备数据\n",
    "    \"\"\"\n",
    "    highs = data.high\n",
    "    start_t = datetime.datetime.now()\n",
    "    print(start_t)\n",
    "\n",
    "    # 斜率\n",
    "    data['beta'] = 0\n",
    "    data['R2'] = 0\n",
    "    beta_rsquared = np.zeros((len(data), 2), )\n",
    "\n",
    "    for i in range(N - 1, len(highs) - 1):\n",
    "        # for i in range(len(highs))[N:]:\n",
    "        df_ne = data.iloc[i - N + 1:i + 1, :]\n",
    "        model = sml.ols(formula='high~low', data=df_ne)\n",
    "        result = model.fit()\n",
    "\n",
    "        # beta = low\n",
    "        beta_rsquared[i + 1, 0] = result.params[1]\n",
    "        beta_rsquared[i + 1, 1] = result.rsquared\n",
    "\n",
    "    data[['beta', 'R2']] = beta_rsquared\n",
    "\n",
    "    # 日收益率\n",
    "    data['ret'] = data.close.pct_change(1)\n",
    "\n",
    "    # 标准分\n",
    "    # data['beta_norm'] = (data['beta'] - data.beta.rolling(M).mean().shift(1))\n",
    "    # / data.beta.rolling(M).std().shift(1)\n",
    "    data['beta_norm'] = data['beta'].rolling(M).apply(lambda x: scs.zscore(x.values)[-1])\n",
    "\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    data.iloc[:min(M, len(highs)), beta_norm] = scs.zscore(data.iloc[:min(M, len(highs)), beta].values)\n",
    "    # beta_norm = data.columns.get_loc('beta_norm')\n",
    "    # beta = data.columns.get_loc('beta')\n",
    "    # for i in range(min(M, len(highs))):\n",
    "    # data.iat[i, beta_norm] = (data.iat[i, beta] - data.iloc[:i - 1,\n",
    "    # beta].mean()) / data.iloc[:i - 1, beta].std() if (data.iloc[:i - 1,\n",
    "    # beta].std() != 0) else np.nan\n",
    "\n",
    "    # data.iat[2, beta_norm] = 0\n",
    "    data['RSRS_R2'] = data.beta_norm * data.R2\n",
    "    data = data.fillna(0)\n",
    "\n",
    "    # 右偏标准分\n",
    "    data['beta_right'] = data.RSRS_R2 * data.beta\n",
    "    \n",
    "    end_t = datetime.datetime.now()\n",
    "    print(end_t, 'spent:{}'.format((end_t - start_t)))\n",
    "\n",
    "    return data\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def RSRS1(price, S1=1.0, S2=0.8):\n",
    "    \"\"\"\n",
    "    斜率指标交易策略标准分策略\n",
    "    \"\"\"\n",
    "    data = price.copy()\n",
    "    data['flag'] = 0  # 买卖标记\n",
    "    data['position'] = 0  # 持仓标记\n",
    "\n",
    "    beta = data.columns.get_loc('beta')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0  # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1, data.shape[0] - 1):\n",
    "        # 开仓\n",
    "        if data.iat[i, beta] > S1 and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "\n",
    "        # 平仓\n",
    "        elif data.iat[i, beta] < S2 and position == 1:\n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0\n",
    "            position = 0\n",
    "\n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]\n",
    "\n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod()\n",
    "\n",
    "    return (data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "def RSRS2(price,\n",
    "          S=0.7):\n",
    "    \"\"\"\n",
    "    标准分策略\n",
    "    \"\"\"\n",
    "    data = price.copy()\n",
    "    data['flag'] = 0  # 买卖标记\n",
    "    data['position'] = 0  # 持仓标记\n",
    "\n",
    "    beta_norm = data.columns.get_loc('beta_norm')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0  # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1, data.shape[0] - 1):\n",
    "        # 开仓\n",
    "        if data.iat[i, beta_norm] > S and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "        # 平仓\n",
    "        elif data.iat[i, beta_norm] < -S and position == 1:\n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0\n",
    "            position = 0\n",
    "\n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]\n",
    "\n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod()\n",
    "\n",
    "    return (data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "def RSRS3(HS300, S=0.7):\n",
    "    \"\"\"\n",
    "    修正标准分策略\n",
    "    \"\"\"\n",
    "    data = HS300.copy()\n",
    "    data['flag'] = 0  # 买卖标记\n",
    "    data['position'] = 0  # 持仓标记\n",
    "\n",
    "    RSRS_R2 = data.columns.get_loc('RSRS_R2')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0  # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1, data.shape[0] - 1):\n",
    "        # 开仓\n",
    "        if data.iat[i, RSRS_R2] > S and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "        # 平仓\n",
    "        elif data.iat[i, RSRS_R2] < -S and position == 1:\n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0\n",
    "            position = 0\n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]\n",
    "\n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod()\n",
    "\n",
    "    return (data)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def RSRS4(price,\n",
    "          S=0.7):\n",
    "    \"\"\"\n",
    "    #右偏标准分策略\n",
    "    \"\"\"\n",
    "    data = price.copy()\n",
    "    data['flag'] = 0  # 买卖标记\n",
    "    data['position'] = 0  # 持仓标记\n",
    "\n",
    "    beta_right = data.columns.get_loc('beta_right')\n",
    "    flag = data.columns.get_loc('flag')\n",
    "    position_col = data.columns.get_loc('position')\n",
    "\n",
    "    position = 0  # 是否持仓，持仓：1，不持仓：0\n",
    "    for i in range(1, data.shape[0] - 1):\n",
    "\n",
    "        # 开仓\n",
    "        if data.iat[i, beta_right] > S and position == 0:\n",
    "            data.iat[i, flag] = 1\n",
    "            data.iat[i + 1, position_col] = 1\n",
    "            position = 1\n",
    "        # 平仓\n",
    "        elif data.iat[i, beta_right] < -S and position == 1:\n",
    "            data.iat[i, flag] = -1\n",
    "            data.iat[i + 1, position_col] = 0\n",
    "            position = 0\n",
    "\n",
    "        # 保持\n",
    "        else:\n",
    "            data.iat[i + 1, position_col] = data.iat[i, position_col]\n",
    "\n",
    "    data['nav'] = (1 + data.close.pct_change(1).fillna(0) * data.position).cumprod()\n",
    "\n",
    "    return (data)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "codelist = ['600519']\n",
    "# codelist = ['600239']\n",
    "# codelist = ['600338']\n",
    "# codelist = ['000671']\n",
    "#     codelist = ['600095','600822','600183']\n",
    "\n",
    "# 获取股票中文名称，只是为了看得方便，交易策略并不需要股票中文名称\n",
    "stock_names = QA.QA_fetch_stock_name(codelist)\n",
    "codename = [stock_names.at[code, 'name'] for code in codelist]\n",
    "\n",
    "data_day = QA.QA_fetch_stock_day_adv(codelist,\n",
    "                                     '2020-01-01',\n",
    "                                     '{}'.format(datetime.date.today(), )).to_qfq()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>code</th>\n",
       "      <th>decimal_point</th>\n",
       "      <th>name</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>sec</th>\n",
       "      <th>sse</th>\n",
       "      <th>volunit</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>code</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>600519</th>\n",
       "      <td>600519</td>\n",
       "      <td>2</td>\n",
       "      <td>贵州茅台</td>\n",
       "      <td>1733.0</td>\n",
       "      <td>stock_cn</td>\n",
       "      <td>sh</td>\n",
       "      <td>100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          code  decimal_point  name  pre_close       sec sse  volunit\n",
       "code                                                                 \n",
       "600519  600519              2  贵州茅台     1733.0  stock_cn  sh      100"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stock_names"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-09-15 16:17:08.999942\n",
      "2020-09-15 16:17:10.633815 spent:0:00:01.633873\n"
     ]
    }
   ],
   "source": [
    "indices_rsrsT = data_day.add_func(pre_rsrs_data_v1_func)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-09-15 16:17:29.525525 spent:0:00:01.339024\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/zhangjx/anaconda3/envs/qawork/lib/python3.7/site-packages/ipykernel_launcher.py:31: FutureWarning: Currently, 'apply' passes the values as ndarrays to the applied function. In the future, this will change to passing it as Series objects. You need to specify 'raw=True' to keep the current behaviour, and you can pass 'raw=False' to silence this warning\n"
     ]
    }
   ],
   "source": [
    "indices_rsrs2 = data_day.add_func(pre_rsrs_data_v2_func)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "154\n"
     ]
    }
   ],
   "source": [
    "data=data_day.data.dropna()\n",
    "print(len(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/zhangjx/anaconda3/envs/qawork/lib/python3.7/site-packages/ipykernel_launcher.py:5: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  \"\"\"\n",
      "/home/zhangjx/anaconda3/envs/qawork/lib/python3.7/site-packages/ipykernel_launcher.py:6: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy\n",
      "  \n"
     ]
    }
   ],
   "source": [
    "highs = data.high.values\n",
    "lows = data.low.values\n",
    "N=18\n",
    "M=252\n",
    "data['beta'] = 0\n",
    "data['R2'] = 0\n",
    "beta_rsquared = np.zeros((len(data), 2), )\n",
    "for i in range(len(highs))[N:]:\n",
    "    data_high = highs[i - N:i]\n",
    "    data_low = lows[i - N:i]\n",
    "    X = sm.add_constant(data_low)\n",
    "    model = sm.OLS(data_high, X)\n",
    "#     results = model.fit()\n",
    "\n",
    "#     # beta = low\n",
    "#     if (len(results.params) > 1):\n",
    "#         beta_rsquared[i, 0] = results.params[1]\n",
    "#     else:\n",
    "#         beta_rsquared[i, 0] = results.params[0]\n",
    "#     beta_rsquared[i, 1] = results.rsquared\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2020-09-15 16:18:44.192109\n",
      "2020-09-15 16:18:44.328965\n"
     ]
    },
    {
     "ename": "MissingDataError",
     "evalue": "exog contains inf or nans",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mMissingDataError\u001b[0m                          Traceback (most recent call last)",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, *args, **kwargs)\u001b[0m\n\u001b[1;32m    688\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 689\u001b[0;31m                 \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_python_apply_general\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    690\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36m_python_apply_general\u001b[0;34m(self, f)\u001b[0m\n\u001b[1;32m    706\u001b[0m         keys, values, mutated = self.grouper.apply(f, self._selected_obj,\n\u001b[0;32m--> 707\u001b[0;31m                                                    self.axis)\n\u001b[0m\u001b[1;32m    708\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/pandas/core/groupby/ops.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, f, data, axis)\u001b[0m\n\u001b[1;32m    189\u001b[0m             \u001b[0mgroup_axes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_get_axes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 190\u001b[0;31m             \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    191\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_is_indexed_like\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgroup_axes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-35-590d7f59eb73>\u001b[0m in \u001b[0;36mpre_rsrs_data_func\u001b[0;34m(data, N, M)\u001b[0m\n\u001b[1;32m     18\u001b[0m         \u001b[0mX\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_constant\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_low\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m         \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mOLS\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_high\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     20\u001b[0m         \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/regression/linear_model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m    816\u001b[0m         super(OLS, self).__init__(endog, exog, missing=missing,\n\u001b[0;32m--> 817\u001b[0;31m                                   hasconst=hasconst, **kwargs)\n\u001b[0m\u001b[1;32m    818\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0;34m\"weights\"\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_init_keys\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/regression/linear_model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, weights, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m    662\u001b[0m         super(WLS, self).__init__(endog, exog, missing=missing,\n\u001b[0;32m--> 663\u001b[0;31m                                   weights=weights, hasconst=hasconst, **kwargs)\n\u001b[0m\u001b[1;32m    664\u001b[0m         \u001b[0mnobs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/regression/linear_model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, **kwargs)\u001b[0m\n\u001b[1;32m    178\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 179\u001b[0;31m         \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mRegressionModel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    180\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data_attr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'pinv_wexog'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'wendog'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'wexog'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'weights'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, **kwargs)\u001b[0m\n\u001b[1;32m    211\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 212\u001b[0;31m         \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mLikelihoodModel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    213\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minitialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, **kwargs)\u001b[0m\n\u001b[1;32m     63\u001b[0m         self.data = self._handle_data(endog, exog, missing, hasconst,\n\u001b[0;32m---> 64\u001b[0;31m                                       **kwargs)\n\u001b[0m\u001b[1;32m     65\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mk_constant\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mk_constant\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/model.py\u001b[0m in \u001b[0;36m_handle_data\u001b[0;34m(self, endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m     86\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_handle_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmissing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhasconst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 87\u001b[0;31m         \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhandle_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmissing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhasconst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     88\u001b[0m         \u001b[0;31m# kwargs arrays could have changed, easier to just attach here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/data.py\u001b[0m in \u001b[0;36mhandle_data\u001b[0;34m(endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m    632\u001b[0m     return klass(endog, exog=exog, missing=missing, hasconst=hasconst,\n\u001b[0;32m--> 633\u001b[0;31m                  **kwargs)\n\u001b[0m",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/data.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m     78\u001b[0m         \u001b[0;31m# this has side-effects, attaches k_constant and const_idx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 79\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle_constant\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhasconst\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     80\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_check_integrity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/data.py\u001b[0m in \u001b[0;36m_handle_constant\u001b[0;34m(self, hasconst)\u001b[0m\n\u001b[1;32m    132\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misfinite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mptp_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 133\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mMissingDataError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'exog contains inf or nans'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    134\u001b[0m             \u001b[0mconst_idx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwhere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mptp_\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msqueeze\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mMissingDataError\u001b[0m: exog contains inf or nans",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mMissingDataError\u001b[0m                          Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-36-f272c5c3134d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mindices_rsrs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata_day\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_func\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpre_rsrs_data_func\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/QUANTAXIS/QAData/base_datastruct.py\u001b[0m in \u001b[0;36madd_func\u001b[0;34m(self, func, *arg, **kwargs)\u001b[0m\n\u001b[1;32m   1043\u001b[0m         \"\"\"\n\u001b[1;32m   1044\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1045\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgroupby\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlevel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msort\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1046\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1047\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0madd_funcx\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m*\u001b[0m\u001b[0marg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, func, *args, **kwargs)\u001b[0m\n\u001b[1;32m    699\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    700\u001b[0m                 \u001b[0;32mwith\u001b[0m \u001b[0m_group_selection_context\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 701\u001b[0;31m                     \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_python_apply_general\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    702\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    703\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/pandas/core/groupby/groupby.py\u001b[0m in \u001b[0;36m_python_apply_general\u001b[0;34m(self, f)\u001b[0m\n\u001b[1;32m    705\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_python_apply_general\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    706\u001b[0m         keys, values, mutated = self.grouper.apply(f, self._selected_obj,\n\u001b[0;32m--> 707\u001b[0;31m                                                    self.axis)\n\u001b[0m\u001b[1;32m    708\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    709\u001b[0m         return self._wrap_applied_output(\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/pandas/core/groupby/ops.py\u001b[0m in \u001b[0;36mapply\u001b[0;34m(self, f, data, axis)\u001b[0m\n\u001b[1;32m    188\u001b[0m             \u001b[0;31m# group might be modified\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    189\u001b[0m             \u001b[0mgroup_axes\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_get_axes\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 190\u001b[0;31m             \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgroup\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    191\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0m_is_indexed_like\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgroup_axes\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    192\u001b[0m                 \u001b[0mmutated\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-35-590d7f59eb73>\u001b[0m in \u001b[0;36mpre_rsrs_data_func\u001b[0;34m(data, N, M)\u001b[0m\n\u001b[1;32m     17\u001b[0m         \u001b[0mdata_low\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlows\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mN\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m         \u001b[0mX\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd_constant\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_low\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 19\u001b[0;31m         \u001b[0mmodel\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mOLS\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdata_high\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mX\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     20\u001b[0m         \u001b[0mresults\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmodel\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     21\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/regression/linear_model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m    815\u001b[0m                  **kwargs):\n\u001b[1;32m    816\u001b[0m         super(OLS, self).__init__(endog, exog, missing=missing,\n\u001b[0;32m--> 817\u001b[0;31m                                   hasconst=hasconst, **kwargs)\n\u001b[0m\u001b[1;32m    818\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0;34m\"weights\"\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_init_keys\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    819\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_init_keys\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"weights\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/regression/linear_model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, weights, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m    661\u001b[0m             \u001b[0mweights\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mweights\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msqueeze\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    662\u001b[0m         super(WLS, self).__init__(endog, exog, missing=missing,\n\u001b[0;32m--> 663\u001b[0;31m                                   weights=weights, hasconst=hasconst, **kwargs)\n\u001b[0m\u001b[1;32m    664\u001b[0m         \u001b[0mnobs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    665\u001b[0m         \u001b[0mweights\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mweights\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/regression/linear_model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, **kwargs)\u001b[0m\n\u001b[1;32m    177\u001b[0m     \"\"\"\n\u001b[1;32m    178\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 179\u001b[0;31m         \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mRegressionModel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    180\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data_attr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mextend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'pinv_wexog'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'wendog'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'wexog'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'weights'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    181\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, **kwargs)\u001b[0m\n\u001b[1;32m    210\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    211\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 212\u001b[0;31m         \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mLikelihoodModel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    213\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minitialize\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    214\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/model.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, **kwargs)\u001b[0m\n\u001b[1;32m     62\u001b[0m         \u001b[0mhasconst\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'hasconst'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     63\u001b[0m         self.data = self._handle_data(endog, exog, missing, hasconst,\n\u001b[0;32m---> 64\u001b[0;31m                                       **kwargs)\n\u001b[0m\u001b[1;32m     65\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mk_constant\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mk_constant\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     66\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexog\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexog\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/model.py\u001b[0m in \u001b[0;36m_handle_data\u001b[0;34m(self, endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m     85\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     86\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_handle_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmissing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhasconst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 87\u001b[0;31m         \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhandle_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmissing\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhasconst\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     88\u001b[0m         \u001b[0;31m# kwargs arrays could have changed, easier to just attach here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     89\u001b[0m         \u001b[0;32mfor\u001b[0m \u001b[0mkey\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/data.py\u001b[0m in \u001b[0;36mhandle_data\u001b[0;34m(endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m    631\u001b[0m     \u001b[0mklass\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mhandle_data_class_factory\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mendog\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexog\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    632\u001b[0m     return klass(endog, exog=exog, missing=missing, hasconst=hasconst,\n\u001b[0;32m--> 633\u001b[0;31m                  **kwargs)\n\u001b[0m",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/data.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, endog, exog, missing, hasconst, **kwargs)\u001b[0m\n\u001b[1;32m     77\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     78\u001b[0m         \u001b[0;31m# this has side-effects, attaches k_constant and const_idx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 79\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_handle_constant\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhasconst\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     80\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_check_integrity\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     81\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_cache\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresettable_cache\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/anaconda3/envs/qawork/lib/python3.7/site-packages/statsmodels/base/data.py\u001b[0m in \u001b[0;36m_handle_constant\u001b[0;34m(self, hasconst)\u001b[0m\n\u001b[1;32m    131\u001b[0m             \u001b[0mptp_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mexog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mptp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    132\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misfinite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mptp_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mall\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 133\u001b[0;31m                 \u001b[0;32mraise\u001b[0m \u001b[0mMissingDataError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'exog contains inf or nans'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    134\u001b[0m             \u001b[0mconst_idx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwhere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mptp_\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msqueeze\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    135\u001b[0m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mk_constant\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mconst_idx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mMissingDataError\u001b[0m: exog contains inf or nans"
     ]
    }
   ],
   "source": [
    "\"\"\"\n",
    "准备数据\n",
    "\"\"\"\n",
    "highs = data.high.values\n",
    "lows = data.low.values\n",
    "start_t = datetime.datetime.now()\n",
    "print(start_t)\n",
    "\n",
    "# 斜率\n",
    "data['beta'] = 0\n",
    "data['R2'] = 0\n",
    "beta_rsquared = np.zeros((len(data), 2), )\n",
    "\n",
    "for i in range(len(highs))[N:]:\n",
    "    data_high = highs[i - N:i]\n",
    "    data_low = lows[i - N:i]\n",
    "    X = sm.add_constant(data_low)\n",
    "    model = sm.OLS(data_high, X)\n",
    "    results = model.fit()\n",
    "\n",
    "    # beta = low\n",
    "    if (len(results.params) > 1):\n",
    "        beta_rsquared[i, 0] = results.params[1]\n",
    "    else:\n",
    "        beta_rsquared[i, 0] = results.params[0]\n",
    "    beta_rsquared[i, 1] = results.rsquared\n",
    "\n",
    "data[['beta', 'R2']] = beta_rsquared\n",
    "\n",
    "# 日收益率\n",
    "data['ret'] = data.close.pct_change(1)\n",
    "\n",
    "# 标准分\n",
    "data['beta_norm'] = data['beta'].rolling(M).apply(lambda x: scs.zscore(x.values)[-1])\n",
    "\n",
    "beta = data.columns.get_loc('beta')\n",
    "beta_norm = data.columns.get_loc('beta_norm')\n",
    "data.iloc[:min(M, len(highs)), beta_norm] = scs.zscore(data.iloc[:min(M, len(highs)), beta].values)\n",
    "data['RSRS_R2'] = data.beta_norm * data.R2\n",
    "data = data.fillna(0)\n",
    "\n",
    "# 右偏标准分\n",
    "data['beta_right'] = data.RSRS_R2 * data.beta\n",
    "\n",
    "end_t = datetime.datetime.now()\n",
    "print(end_t, 'spent:{}'.format((end_t - start_t)))\n",
    "return data\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 获取ETF/股票中文名称，只是为了看得方便，交易策略并不需要ETF/股票中文名称\n",
    "# stock_names = QA.QA_fetch_etf_name(codelist)\n",
    "# codename = [stock_names.at[code, 'name'] for code in codelist]\n",
    "\n",
    "## 读取 ETF基金 日线，存在index_day中\n",
    "# data_day = QA.QA_fetch_index_day_adv(codelist,\n",
    "#    start='2010-01-01',\n",
    "#    end='{}'.format(datetime.date.today()))\n",
    "\n",
    "codelist = ['600519']\n",
    "# codelist = ['600239']\n",
    "# codelist = ['600338']\n",
    "# codelist = ['000671']\n",
    "#     codelist = ['600095','600822','600183']\n",
    "\n",
    "# 获取股票中文名称，只是为了看得方便，交易策略并不需要股票中文名称\n",
    "stock_names = QA.QA_fetch_stock_name(codelist)\n",
    "codename = [stock_names.at[code, 'name'] for code in codelist]\n",
    "\n",
    "data_day = QA.QA_fetch_stock_day_adv(codelist,\n",
    "                                     '2008-01-01',\n",
    "                                     '{}'.format(datetime.date.today(), )).to_qfq()\n",
    "\n",
    "indices_rsrsT = data_day.add_func(pre_rsrs_data_v1_func)\n",
    "resultT = RSRS1(indices_rsrsT)\n",
    "num = resultT.flag.abs().sum() / 2\n",
    "nav = resultT.nav[resultT.shape[0] - 1]\n",
    "print('RSRS1_T 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "\n",
    "resultT2 = RSRS2(indices_rsrsT)\n",
    "num = resultT2.flag.abs().sum() / 2\n",
    "nav = resultT2.nav[resultT2.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS2_T 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "\n",
    "resultT3 = RSRS3(indices_rsrsT)\n",
    "num = resultT3.flag.abs().sum() / 2\n",
    "nav = resultT3.nav[resultT.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS3_T 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "\n",
    "resultT4 = RSRS4(indices_rsrsT)\n",
    "num = resultT4.flag.abs().sum() / 2\n",
    "nav = resultT4.nav[resultT.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS4_T 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "\n",
    "indices_rsrs = data_day.add_func(pre_rsrs_data_func)\n",
    "result = RSRS1(indices_rsrs)\n",
    "print(indices_rsrs)\n",
    "\n",
    "##斜率数据分布\n",
    "# plt.figure(figsize=(15,5))\n",
    "# plt.hist(indices['beta'], bins= 100, range= None, normed= False, weights=\n",
    "# None, cumulative= False,\n",
    "#         bottom= None, histtype= 'bar', align= 'mid', orientation=\n",
    "#         'vertical', rwidth= None, log= False, color= 'g',\n",
    "#         label='直方图', stacked= False)\n",
    "\n",
    "##RSRS标准分和右偏变准分分布\n",
    "# plt.figure(figsize=(15,5))\n",
    "# plt.hist(indices['beta_norm'], bins= 100, range= None, normed= False,\n",
    "# weights= None, cumulative= False,\n",
    "#         bottom= None, histtype= 'bar', align= 'mid', orientation=\n",
    "#         'vertical', rwidth= None, log= False, color= 'g',\n",
    "#         label='直方图', stacked= False)\n",
    "\n",
    "# plt.figure(figsize=(15,5))\n",
    "# plt.hist(indices['RSRS_R2'], bins= 100, range= None, normed= False,\n",
    "# weights= None, cumulative= False,\n",
    "#         bottom= None, histtype= 'bar', align= 'mid', orientation=\n",
    "#         'vertical', rwidth= None, log= False, color= 'g',\n",
    "#         label='直方图', stacked= False)\n",
    "# plt.show()\n",
    "\n",
    "num = result.flag.abs().sum() / 2\n",
    "nav = result.nav[result.shape[0] - 1]\n",
    "print('RSRS1 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "print(result[['close', 'ret', 'beta', 'R2', 'beta_norm', 'RSRS_R2', 'flag', 'position', 'nav']].tail(50))\n",
    "\n",
    "result2 = RSRS2(indices_rsrs)\n",
    "num = result2.flag.abs().sum() / 2\n",
    "nav = result2.nav[result.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS2 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "\n",
    "result3 = RSRS3(indices_rsrs)\n",
    "num = result3.flag.abs().sum() / 2\n",
    "nav = result3.nav[result.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS3 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "\n",
    "result4 = RSRS4(indices_rsrs)\n",
    "num = result4.flag.abs().sum() / 2\n",
    "nav = result4.nav[result.shape[0] - 1]\n",
    "ret_year = (nav - 1)\n",
    "print('RSRS4 交易次数 = ', num)\n",
    "print('策略净值为= ', nav)\n",
    "\n",
    "# xtick = np.arange(0,result.shape[0],int(result.shape[0] / 7))\n",
    "# xticklabel = pd.Series(result.index.date[xtick])\n",
    "xticklabel = result.index.get_level_values(level=0).to_series().apply(lambda x: x.strftime(\"%Y-%m-%d\")[2:16])\n",
    "\n",
    "plt.figure(figsize=(15, 3))\n",
    "fig = plt.axes()\n",
    "plt.plot(np.arange(result.shape[0]), result.nav, label='RSRS1', linewidth=2)\n",
    "plt.plot(np.arange(result.shape[0]), result2.nav, label='RSRS2', linewidth=2)\n",
    "plt.plot(np.arange(result.shape[0]), result3.nav, label='RSRS3', linewidth=2)\n",
    "plt.plot(np.arange(result.shape[0]), result4.nav, label='RSRS4', linewidth=2)\n",
    "plt.plot(np.arange(result.shape[0]), indices_rsrs.close / indices_rsrs.close[0], label=codelist[0], linewidth=2)\n",
    "\n",
    "fig.set_xticks(range(0, len(xticklabel),\n",
    "                     round(len(xticklabel) / 12)))\n",
    "fig.set_xticklabels(xticklabel[::round(len(xticklabel) / 12)],\n",
    "                    rotation=45)\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "qawork",
   "language": "python",
   "name": "qawork"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
